package com.yj.springsecuritydemo01.service;

import com.yj.springsecuritydemo01.mapper.UserMapper;
import com.yj.springsecuritydemo01.pojo.UserEntity;
import com.yj.springsecuritydemo01.pojo.UserInfo;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * UserDetailsService：指定去哪加载用户信息
 */
@Service
public class UserServiceImpl implements UserDetailsService {


    @Resource
    private UserMapper userMapper;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {

        UserInfo userInfo = userMapper.selectUserInfoByUsername(username);
        if (null == userInfo){
            throw new UsernameNotFoundException("用户信息不存在");
        }

        //将查询出来的用户信息保存在UserDetail中
        return new UserEntity(userInfo,null);
    }
}
